package com.controller;

import com.common.ApiRestResponse;
import com.common.Constant;
import com.exception.MallException;
import com.exception.MallExceptionEnum;
import com.github.pagehelper.PageInfo;
import com.pojo.User;
import com.service.UserService;
import com.vo.userVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author 杨冰清
 * @Date 16:02
 * @Description
 */
@Api(tags = "用户登录注册", value = "登录注册")
@RestController
@RequestMapping("/api")

public class UserController {
    @Autowired
    UserService userService;


    @GetMapping("/test")
    @ResponseBody
    public User personalPage() {
        return userService.getUser();
    }

    /**
     * 注册
     */
    @ApiOperation(value = "注册接口", httpMethod = "POST")
    @PostMapping("/register")
    public ApiRestResponse register(@RequestBody userVo userVo) throws MallException {
        if (StringUtils.isEmpty(userVo.getUsername())) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(userVo.getPassword())) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_PASSWORD);
        }
        //密码长度不能少于3位
        if (userVo.getPassword().length() < 3) {
            return ApiRestResponse.error(MallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        userService.register(userVo);
        return ApiRestResponse.success(userVo);
    }

    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @param session
     * @return
     * @throws MallException
     */
    @ApiOperation("用户登录接口（废弃）")
    @PostMapping("/login")
    public ApiRestResponse login(@RequestParam("username") String username,
                                 @RequestParam("password") String password,
                                 HttpSession session)
            throws MallException {
        System.out.println(username);
        System.out.println(password);
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_PASSWORD);
        }
        User user = userService.login(username, password);
        //保存用户信息时，不保存密码
        user.setPassword(null);
        session.setAttribute(Constant.TEST_MALL_USER, user);
//        session.setMaxInactiveInterval(5);
        System.out.println(Constant.TEST_MALL_USER);
        return ApiRestResponse.success(user);
    }
    @ApiOperation("用户登录接口")
    @PostMapping("/login2")
    public ApiRestResponse loginIn(@RequestBody userVo userVo, HttpSession session) throws MallException {
        if (StringUtils.isEmpty(userVo.getUsername())){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(userVo.getPassword())){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_PASSWORD);
        }
        User user = userService.loginIn(userVo);
        //保存用户信息时，不保存密码
        user.getId();
        user.setPassword(null);
        session.setAttribute(Constant.TEST_MALL_USER, user);
//        session.setMaxInactiveInterval(5);
        System.out.println(Constant.TEST_MALL_USER);
        return ApiRestResponse.success(user);
    }


    /**
     * 修改个性签名
     *
     * @param session
     * @param signature
     * @return
     * @throws MallException
     */
    @ApiOperation("修改个性签名接口")
    @PostMapping("/user/signature")
    public ApiRestResponse updateUserInfo(HttpSession session, @RequestParam String signature)
            throws MallException {
        User currentUser = (User) session.getAttribute(Constant.TEST_MALL_USER);
        System.out.println(currentUser);
        System.out.println(session);
        System.out.println(Constant.TEST_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);
        return ApiRestResponse.success(user);
    }

    @ApiOperation("修改手机号接口")
    @PostMapping("/user/tellphone")
    public ApiRestResponse updateUserTell(HttpSession session,@RequestParam String tellphone)throws MallException{
        User currentUser = (User) session.getAttribute(Constant.TEST_MALL_USER);
        if (currentUser == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setTellPhone(tellphone);
        userService.updateUserTell(user);
        return ApiRestResponse.success(user);
    }
    @ApiOperation("修改密码接口")
    @PostMapping("/user/updatePass")
    public ApiRestResponse updatePass(HttpSession session,@RequestParam String password,
                                      @RequestParam String repassword )throws MallException{
        User currentUser = (User) session.getAttribute(Constant.TEST_MALL_USER);
        if (currentUser == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        if (password.equals(repassword)){
            User user = new User();
            user.setId(currentUser.getId());
            user.setPassword(password);
            userService.updatePass(user);
            return ApiRestResponse.success(user);
        }else {
            return ApiRestResponse.error(MallExceptionEnum.DIFF_PASSWORD);
        }


    }



    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @ApiOperation("退出登录接口")
    @PostMapping("/user/logout")
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.TEST_MALL_USER);
        return ApiRestResponse.success();
    }

    /**
     * 管理员登录接口（废弃）
     *
     * @param username
     * @param password
     * @param session
     * @return
     * @throws MallException
     */
    @ApiOperation("用户登录接口（废弃）")
    @PostMapping("/adminLogin")
    public ApiRestResponse adminLogin(@RequestParam("username") String username,
                                      @RequestParam("password") String password, HttpSession session)
            throws MallException {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_PASSWORD);
        }
        User user = userService.login(username, password);
        //校验是否是管理员
        if (userService.checkAdminRole(user)) {
            //是管理员，执行操作
            //保存用户信息时，不保存密码
            user.setPassword(null);
            session.setAttribute(Constant.TEST_MALL_USER, user);
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
    }

    /**
     * 管理员登录接口（新版）
     *
     * @param username
     * @param password
     * @return
     * @throws MallException
     */
    /*@ApiOperation("管理员登录接口（新版）")
    @PostMapping("/adminLog")
    public ApiRestResponse adminLog(@RequestParam("username") String username,
                                    @RequestParam("password") String password) throws MallException {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_PASSWORD);
        }
        if (username.equals("admin") && password.equals("admin")) {
            return ApiRestResponse.success();
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
    }*/

    /**
     * 分页查询用户接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("后台用户列表接口（分页）")
    @GetMapping("/admin/list")
    public ApiRestResponse list(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "6", required = false) Integer pageSize) {
        PageInfo pageInfo = userService.AdminListForUser(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    /**
     * 查询用户不分页
     *
     * @return
     */
    @ApiOperation("后台用户列表接口(不分页)")
    @GetMapping("/admin/list2")
    public ApiRestResponse listNo() {
        List<User> list = userService.list();
        return ApiRestResponse.success(list);
    }

    @ApiOperation("查询用户个人信息")
    @GetMapping("/user/msg")
    public ApiRestResponse listUserMsg(Integer id){
        User user = userService.listUserMsg(id);
        return  ApiRestResponse.success(user);
    }

}
